﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using SqlMiM.Common.Interfaces;
using System.Reflection;
using System.Globalization;

namespace SqlMiM.Common.Framework.Configuration
{
    public class ConfigurationObject : INotifyPropertyChanged
    {
        
        public SortedList  Properties { get; set; }
        public String PluginName { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public ConfigurationObject(String pluginName)
        {
            this.Properties = new SortedList();
            this.PluginName = pluginName;
        }

        public object GetProperty(string name)
        {
            return this.GetProperty(name, null);
        }

        public object GetProperty(string name, object defaultValue)
        {
            if (this.Properties.Contains(name))
            {
                return this.Properties[name];
            }
            return defaultValue;
        }

        public void SetProperty(string name, object value)
        {
            if (!this.Properties.Contains(name))
            {
                this.Properties.Add(name, value);
                this.RaisePropertyChanged(new PropertyChangedEventArgs(name));
                return;
            }

            if (!this.Properties[name].Equals(value))
            {
                this.Properties[name] = value;
                this.RaisePropertyChanged(new PropertyChangedEventArgs(name));

            }
        }

        public bool HasProperty(string name)
        {
            return this.Properties.Contains(name);
        }

        /// <summary>
        /// Load a value from a text value and a type text
        /// </summary>
        internal void Load(string strName, string strType, string strValue)
        {
            Type type = null;

            if (string.IsNullOrEmpty(strType))
            {
                type = typeof(string);
            }
            else
            {
                foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    type = assembly.GetType(strType);
                    if (type != null)
                    {
                        break;
                    }
                }
            }

            if (type != null)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(type);
                if (converter != null)
                {
                    try
                    {
                        object newVal = converter.ConvertFromString(null, CultureInfo.InvariantCulture, strValue);
                        this.SetProperty(strName, newVal);
                    }
                    catch (NotSupportedException)
                    {
                    }
                }
            }
        }

        protected virtual void RaisePropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }


    }


}
